﻿//#define MusicDebug
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using JSON;
public class Music {
	static public string PathBGM = "Music/BGM/";
	static public string PathSound = "Music/Sound/";
	static Transform m_AudioListenerTR;
	static AudioSource BGMSound = null;
	#region 通用UI事件
	/// <summary>
	/// 点击任意按钮 除非特殊情况 否则播放这个音效
	/// </summary>
	public static void Play2DUIButton(){
		Music.PlaySound ("通用按钮点击时");
	}
	/// <summary>
	/// 通用界面滑入时候 播放的音效
	/// 选歌界面
	/// 模式选择界面
	/// </summary>
	public static void PlayPanelFlyingEnter(){
		Music.PlaySound ("通用界面滑入");
	}
	/// <summary>
	/// 通用界面滑出时候 播放的音效
	/// 选歌界面
	/// 模式选择界面
	/// </summary>
	public static void PlayPanelFlyingExit(){
		Music.PlaySound ("通用界面滑出");
	}
	/// <summary>
	/// 通用弹出框弹出
	/// </summary>
	public static void PlayPanelPopOpen(){
		Music.PlaySound ("通用弹出框弹出");
	}
	/// <summary>
	/// 通用弹出框关闭
	/// </summary>
	public static void PlayPanelPopClose(){
		Music.PlaySound ("通用弹出框关闭");
	}
	#endregion


	public static void PlayBGMLoop(float[] looptime,float yieldtime){
		MusicLoop.Init (BGMSound, looptime,yieldtime);
	}
	public static void SetBGMTime(float highttime){
		BGMSound.time = highttime;
	}
	public static void SetBGM(AudioSource bgm){
		BGMSound = bgm;
	}
	public static void DespawnBGM(){
		BGMSound.gameObject.name = "MusicSound";
		ObjManager.Instance.Despawn (BGMSound.gameObject,0);
//		GTool.Despawn (BGMSound.transform);
	}
	public static void GCBGM(){
		BGMSound = null;
	}

	/// <summary>
	/// 平缓的小声
	/// </summary>
	public static void SmoothSlowUpBGM(float time =0.5f){
		SmoothSlow (BGMSound,1,time);
	}
	/// <summary>
	/// 平缓的大声
	/// </summary>
	public static void SmoothSlowDownBGM(float time = 0.5f){
		SmoothSlow (BGMSound,0,time);
	}
	public static void SmoothSlow(AudioSource target,float to,float time = 0.5f){
		if (target == null)
			return;
		if (to == 0)
			target.volume = 1;
		else
			target.volume = 0;
//		iTween.AudioTo (target.gameObject, to, 1, time);
	}
	public static AudioSource PlayBGM(string musicname){
		return PlayBGM(PathBGM,musicname,AudioLisitionTR);
	}
	public static AudioSource PlaySound(string musicname){
		return PlaySound(PathSound,musicname,AudioLisitionTR);
	}
	public static void PlayAudioSoure(AudioSource sound){
		ObjManager.SetParentAndRest(sound.transform,AudioLisitionTR);
		sound.Play();
	}
	public static AudioSource GetBGMSound(){
		return BGMSound;
	}
	public static void PauseBGM(){
		if (BGMSound) {
			if(BGMSound.isPlaying)
				BGMSound.Pause ();
			else
				BGMSound.Play ();
		}
	}

	public static float BMGLength{
		get{
			if (BGMSound)
				return BGMSound.clip.length;
			return 0;
		}
	}
	/// <summary>
	/// 设置音速
	/// </summary>
	/// <param name="value">Value.</param>
	public static void SetBGMPitch(float value){
		if (BGMSound)
			BGMSound.pitch = value;
	}
	public static void StopBGM(){
		if (BGMSound) {
			BGMSound.Stop ();
			BGMSound.time = 0;
		}
	}
	public static void PlayBGM(){
		if(BGMSound)
		BGMSound.Play ();
	}
	public static void PlayBGM(float startpos){
		if (BGMSound) {
			BGMSound.time = startpos;
			BGMSound.Play ();
		}

	}
	public static Transform AudioLisitionTR{
		set{
			m_AudioListenerTR = value;
		}
		get{	
			if(m_AudioListenerTR == null){
				try{
					m_AudioListenerTR = GameObject.FindObjectOfType<AudioListener>().transform;
				}
				catch{
					m_AudioListenerTR = new GameObject("AudioListener").AddComponent<AudioListener>().transform;
				}
			}
			return m_AudioListenerTR;
		}
	}
	static AudioSource PlayBGM(string path,string musicname,Transform audioparent){
		BGMSound = PlayMusic(path,musicname,audioparent,BGMSound,false);
		return BGMSound;
	}
	static AudioSource PlaySound(string path,string musicname,Transform audioparent){
		return PlayMusic(path,musicname,audioparent);
	}
	static AudioSource PlayMusic(string path,string Musicname,Transform parent){
		return PlayMusic (path, Musicname, parent, null);
	}
	public static AudioSource PlaySoundDontAutoDespawn(string Musicname){
		string clipname = JSONMusicData.getInstance ().GetPrefabName (Musicname);
		AudioClip clip;
		AudioSource audiosource = null;
		clip = LoadClip (PathSound + clipname);
		if (clip == null) {
#if UNITY_EDITOR_WIN && MusicDebug
			if(JSONMusicData.getInstance().GetPrefabName(Musicname) == ""){
				Debug.Log("傻了吧？听不到音效了吧？是不是又要喊程序了？先自个看看配置表  MusicData 里有没有第一列有没有>>>>  "+Musicname+"！！！！");
			}else{
				Debug.Log("你是不是傻？你吖的又忘了把音乐资源丢进来了？看看这个路径吧>>>>>   "+(PathSound+JSONMusicData.getInstance().GetPrefabName(Musicname))+"   <<<<找不到名为:>>>   "+Musicname+"   <<<的音效,");
			}
#endif
			return audiosource;
		}
		audiosource = GetAudioSource (AudioLisitionTR, 0);
		audiosource.clip = clip;
		audiosource.loop = JSONMusicData.getInstance ().GetLoop (Musicname) == 1;
		audiosource.volume = JSONMusicData.getInstance().GetMusicVolume(Musicname);
		audiosource.Play ();
#if UNITY_EDITOR_WIN && MusicDebug
		Debug.Log ("准备播放音效>>>配置名:>>" + Musicname + ">>资源名>>"+audiosource.name+(audiosource.loop == true ? ">>循环>>" : ">>不循环>>") + ">>音量大小>>" + audiosource.volume);
#endif
		return audiosource;
	}
	static AudioSource PlayMusic(string path,string Musicname,Transform parent,AudioSource audiosource,bool doublepath = false){
		AudioClip clip;
		string clipname = JSONMusicData.getInstance ().GetPrefabName (Musicname);
		if(clipname == "xxx")
			return audiosource;
//		if (GameSetting.instance.GAME_LOAD_TYPE == LoadType.AssetBuild) {

//			//bgm
//			clip = ADMusicLoad.getInstace ().LoadAsset (clipname);
//			if (clip == null)
//				return audiosource;
//		} else {
			if (doublepath)
				clipname = clipname + "/" + clipname;
			clip = LoadClip (path + clipname);
//		}
		if(clip == null){
			#if UNITY_EDITOR_WIN && MusicDebug
			if(JSONMusicData.getInstance().GetPrefabName(Musicname) == ""){
				Debug.Log("傻了吧？听不到音效了吧？是不是又要喊程序了？先自个看看配置表  MusicData 里有没有第一列有没有>>>>  "+Musicname+"！！！！");
			}else{
				Debug.Log("你是不是傻？你吖的又忘了把音乐资源丢进来了？看看这个路径吧>>>>>   "+(path+JSONMusicData.getInstance().GetPrefabName(Musicname))+"   <<<<找不到名为:>>>   "+Musicname+"   <<<的音效,");
			}
			#endif
			return audiosource;
		}
		float yt;
		if(JSONMusicData.getInstance().GetLoop(Musicname) == 1 || path == PathBGM)
			yt = 0;
		else
			yt = clip.length;
//		if(json["持续时间"].AsFloat != 0)
//			yt = json["持续时间"].AsFloat;
		if (audiosource == null) {
			audiosource = GetAudioSource (parent, yt);
		}
		audiosource.name = JSONMusicData.getInstance().GetPrefabName(Musicname);
		audiosource.clip = clip;
		audiosource.loop = JSONMusicData.getInstance().GetLoop(Musicname) == 1;
		audiosource.volume = JSONMusicData.getInstance().GetMusicVolume(Musicname);
		audiosource.Play();
		#if UNITY_EDITOR_WIN && MusicDebug
		Debug.Log ("准备播放音效>>>配置名:>>" + Musicname + ">>资源名>>"+audiosource.name+(audiosource.loop == true ? ">>循环>>" : ">>不循环>>") + ">>音量大小>>" + audiosource.volume);
		#endif
		return audiosource;
	}
	static public void DespawnAudioSource(AudioSource audio){
#if UNITY_EDITOR_WIN && MusicDebug
		Debug.Log("准备销毁音效>>"+audio.clip.name);
#endif
		audio.name = "MusicSound";
		ObjManager.Instance.Despawn (audio.gameObject);
//		GTool.Despawn (audio.transform);
	}
	static public void DespawnAudioSource(AudioSource audio,float delaytime){
		#if UNITY_EDITOR_WIN && MusicDebug
		Debug.Log("准备销毁音效>>"+audio.clip.name);
		#endif
		audio.name = "MusicSound";
		ObjManager.Instance.Despawn (audio.gameObject,delaytime);
	}
	static AudioSource GetAudioSource(Transform parent,float yt){
		GameObject obj;
		obj = ObjManager.Instance.AddChild(ObjManager.Instance.PathModel,"MusicSound",parent,ObjManager.Instance.ParentDefualt,yt);
		AudioSource audio;
		audio = obj.GetComponent<AudioSource>();
		return audio;
	}
	/// <summary>
	/// 需要完整路径
	/// </summary>
	/// <returns>The clip.</returns>
	/// <param name="musicpath">Musicpath.</param>
	static public AudioClip LoadClip(string musicpath){

		return Resources.Load<AudioClip>(musicpath);

	}
	static public AudioClip LoadClipFormJSON(string musicname,bool bgm,bool doublename = false){
		string musicpath;
		if (bgm)
			musicpath = PathBGM;
		else
			musicpath = PathSound;
		AudioClip clip;
		string clipname = JSONMusicData.getInstance ().GetPrefabName (musicname);
		if (doublename)
			clipname = clipname + "/" + clipname;
		clip = LoadClip (musicpath + clipname);
		return clip;
	}
}
